package com.eastone.user.serverapi.controller;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eastone.user.serverapi.model.InfoRole;
import com.eastone.user.serverapi.service.InfoRoleService;
import com.yjx.framework.cache.ServiceResultCodeCache;
import com.yjx.framework.constant.Constant;
import com.yjx.framework.model.ServiceResult;
import com.yjx.framework.request.YjxHttpServletRequest;
import com.yjx.framework.util.JsonUtil;
import com.yjx.framework.util.MathUtil;
import com.yjx.framework.util.StrUtil;

@Controller
@RequestMapping(value = "/infoRole")
public class InfoRoleController {

	private static final Logger LOG = LogManager.getLogger(InfoRoleController.class);
	private static ServiceResultCodeCache resultCode = ServiceResultCodeCache.getInstance();
	@Autowired
	private InfoRoleService infoRoleService;

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public @ResponseBody JSONObject get(YjxHttpServletRequest req, @PathVariable("id") int id) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("id=" + id);
		}

		InfoRole user = infoRoleService.get(id);
		JSONObject jsonObject = JsonUtil.getSuccessJsonObject();
		jsonObject.put("data", JSONObject.toJSON(user));
		return jsonObject;
	}

	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	public @ResponseBody JSONObject getList(YjxHttpServletRequest req) {
		String name = req.getStr("name");
		String code = req.getStr("code");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("name=" + name);
			LOG.debug("code=" + code);
		}
		int pageNo = MathUtil.toDefaultInt(req.getInt("pageNo"), Constant.PAGE_NO_DEFAULT);
		int pageSize = MathUtil.toDefaultInt(req.getInt("pageSize"), Constant.PAGE_SIZE_DEFAULT);
		List<InfoRole> infoRoles = infoRoleService.getList(name, code, pageNo, pageSize);
		JSONObject jsonObject = JsonUtil.getSuccessJsonObject();
		jsonObject.put("data", JSONObject.toJSON(infoRoles));
		return jsonObject;
	}

	@RequestMapping(value = "/count", method = RequestMethod.GET)
	public @ResponseBody JSONObject count(YjxHttpServletRequest req) {
		String name = req.getStr("name");
		String code = req.getStr("code");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("name=" + name);
			LOG.debug("code=" + code);
		}
		JSONObject jsonObject = JsonUtil.getSuccessJsonObject();
		jsonObject.put("data", infoRoleService.getCount(name, code));
		return jsonObject;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody JSONObject add(YjxHttpServletRequest req) {
		String name = req.getStr("name");
		String code = req.getStr("code");
		String desc = req.getStr("desc");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("name=" + name);
			LOG.debug("code=" + code);
			LOG.debug("desc" + desc);
		}

		InfoRole role = new InfoRole();
		role.setCode(code);
		role.setDesc(desc);
		role.setName(name);
		ServiceResult<String> serviceResult = infoRoleService.add(role);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody JSONObject update(YjxHttpServletRequest req) {
		int id = req.getInt("id");
		String name = req.getStr("name");
		String code = req.getStr("code");
		String desc = req.getStr("desc");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("id=" + id);
			LOG.debug("name=" + name);
			LOG.debug("code=" + code);
			LOG.debug("desc" + desc);
		}

		InfoRole role = new InfoRole();
		role.setId(id);
		role.setCode(StrUtil.isBlank(code) ? null : code);
		role.setDesc(StrUtil.isBlank(desc) ? null : desc);
		role.setName(StrUtil.isBlank(name) ? null : name);
		ServiceResult<String> serviceResult = infoRoleService.updateSelective(role);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody JSONObject delete(YjxHttpServletRequest req, @PathVariable("id") int id) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("id=" + id);
		}

		InfoRole role = new InfoRole();
		role.setId(id);
		ServiceResult<String> serviceResult = infoRoleService.delete(role);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}
}
